<template>
	<section>
    <!--+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->
    <!--数据字典类型，视图层-->
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters_type">
				<el-form-item>
					<el-input v-model="filters_type.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="highQuery_type">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd_type">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="sdTypes" highlight-current-row v-loading="listLoading_type" @selection-change="selsChange_type" @row-dblclick="dblclick_type" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="sn" label="编码" width="120" sortable>
			</el-table-column>
      <el-table-column prop="name" label="名字" sortable>
      </el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit_type(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel_type(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove_type" :disabled="this.sels_type.length===0">批量删除</el-button>
      <!--分页工具-->
			<el-pagination @size-change="handleSizeChange_type" :page-sizes="[5,10,15,20]" layout="total, sizes, prev, pager, next, jumper" @current-change="handleCurrentChange_type" :current-page="currentPage_type" :page-size="pageSize_type" :total="total_type" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog :title="myTitle" :visible.sync="addOrEditFormVisible_type" :close-on-click-modal="false">
      <el-form :model="addOrEditForm_type" label-width="80px" :rules="addOrEditFormRules" ref="addOrEditForm_type">
        <el-form-item label="编码" prop="sn">
          <el-input v-model="addOrEditForm_type.sn" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="addOrEditForm_type.name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addOrEditFormVisible_type = false">取消</el-button>
				<el-button type="primary" @click.native="addOrEditSubmit_type" :loading="addOrEditLoading_type">提交</el-button>
			</div>
		</el-dialog>
    <!--++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->

    <!--++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->
    <!--数据字典明细，视图层-->
    <!--抽屉，显示明细数据-->
    <el-drawer title="数据字典明细" :visible.sync="drawer_sdDetail" direction="rtl" size="50%">
      <!--工具条-->
      <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
        <el-form :inline="true" :model="filters_detail">
          <el-form-item>
            <el-input v-model="filters_detail.keyword" placeholder="关键字"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" v-on:click="highQuery_detail">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleAdd_detail">新增</el-button>
          </el-form-item>
        </el-form>
      </el-col>

      <!--列表-->
      <el-table :data="sdDetails" highlight-current-row v-loading="listLoading_detail" @selection-change="selsChange_detail">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column type="index" width="60">
        </el-table-column>
        <el-table-column property="name" label="名称"></el-table-column>
        <el-table-column label="操作" width="150">
          <template scope="scope">
            <el-button size="small" @click="handleEdit_detail(scope.$index, scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="handleDel_detail(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--工具条-->
      <el-col :span="24" class="toolbar">
        <el-button type="danger" @click="batchRemove_detail" :disabled="this.sels_detail.length===0">批量删除</el-button>
        <!--分页工具-->
        <el-pagination @size-change="handleSizeChange_detail" :page-sizes="[5,10,15,20]" layout="total, sizes, prev, pager, next, jumper" @current-change="handleCurrentChange_detail" :current-page="currentPage_detail" :page-size="pageSize_detail" :total="total_detail" style="float:right;">
        </el-pagination>
      </el-col>
    </el-drawer>

    <!--编辑界面-->
    <el-dialog :title="myTitle" :visible.sync="addOrEditFormVisible_detail" :close-on-click-modal="false">
      <el-form :model="addOrEditForm_detail" label-width="80px" :rules="addOrEditFormRules" ref="addOrEditForm_detail">
        <el-form-item label="名称" prop="name">
          <el-input v-model="addOrEditForm_detail.name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addOrEditFormVisible_detail = false">取消</el-button>
        <el-button type="primary" @click.native="addOrEditSubmit_detail" :loading="addOrEditLoading_detail">提交</el-button>
      </div>
    </el-dialog>
    <!--+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->
	</section>
</template>

<script>
	export default {
		data() {
			return {
				filters_type: {
          keyword: ''
				},
        //====================================
        //数据字典类型，模型层数据
        sdTypes: [],
				total_type: 0,
        currentPage_type: 1,
        pageSize_type: 5,
        //列表忙等
				listLoading_type: false,
				sels_type: [],//列表选中列

				addOrEditFormVisible_type: false,//编辑界面是否显示
				addOrEditLoading_type: false,    //新增或修改操作，确认按钮忙等
        //编辑界面数据
        addOrEditForm_type: {
          id: null,
          name: "",
          sn: ""
        },
        //数据字典明细抽屉是否打开
        drawer_sdDetail:false,
        //========================================

        //========================================
        //数据字典明细，模型层数据
        filters_detail: {
          keyword: ''
        },
        sdDetails:[],
        listLoading_detail:false,
        sels_detail:[],
        total_detail: 0,
        currentPage_detail: 1,
        pageSize_detail: 5,
        addOrEditFormVisible_detail: false,//编辑界面是否显示
        addOrEditLoading_detail: false,    //新增或修改操作，确认按钮忙等
        //编辑界面数据
        addOrEditForm_detail: {
          id: null,
          name: "",
          types_id: ""
        },



        //=========================================
				addOrEditFormRules: {
					name: [
						{ required: true, message: '请输入名称', trigger: 'blur' }
					],
          sn: [
            { required: true, message: '请输入编码', trigger: 'blur' }
          ]
				},
        myTitle:""
			}
		},
		methods: {
		  //============================================================================
      //数据字典类型，方法
      //高级查询后将当前页跳转至第一页
      highQuery_type(){
        this.currentPage_type = 1;
        this.getSDType();
      },

      //当前页改变时
			handleCurrentChange_type(val) {
				this.currentPage_type = val;
				this.getSDType();
			},
			//获取数据字典类型列表
			getSDType() {
        //设置分页参数，json格式
				let para = {
          currentPage: this.currentPage_type,
          pageSize: this.pageSize_type,
          keyword: this.filters_type.keyword
				};
				this.listLoading_type = true;

				//发送异步请求从后端拿数据
				this.$http.post("/sdtype",para)
				  .then(result=>{
				    //从回调参数中获取data数据
            var data = result.data;
            this.sdTypes = data.rows;
            this.total_type = data.total;
            this.listLoading_type = false;
          })
          .catch(result=>{
            console.log(result);
            this.$message.error('系统繁忙，请稍后重试!!!');
          })
			},
			//删除
			handleDel_type: function (index, row) {
        //是否删除提示框
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {   //确定
					this.listLoading_type = true;
					//NProgress.start();
					this.$http.delete("/sdtype/"+row.id)
            .then(result=>{
              result = result.data;
              if (result.success==false){
                this.$message.error(result.message);
              }
              //删除成功后跳转至第一页
              this.currentPage_type = 1;
              this.getSDType();
              this.listLoading_type = false;
              this.$message.success(result.message);
            })
            .catch(result=>{
              console.log(result);
              this.$message.error('系统繁忙，请稍后重试!!!');
            })
				}).catch(() => {  //取消

				});
			},
			//显示编辑界面
			handleEdit_type: function (index, row) {
        this.myTitle="编辑";
				this.addOrEditFormVisible_type = true;
        //克隆  用于回显
				this.addOrEditForm_type = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd_type: function () {
        this.myTitle="新增";
				this.addOrEditFormVisible_type = true;
        //重置表单
				this.addOrEditForm_type = {
          id: null,
          name: "",
          sn: ""
				};
			},
			//新增或修改确认提交
			addOrEditSubmit_type: function () {
				this.$refs.addOrEditForm_type.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
						  //开启确认按钮忙等
							this.addOrEditLoading_type = true;
							let para = Object.assign({}, this.addOrEditForm_type);

              this.$http.put("/sdtype",para)
                  .then(result=>{
                    result = result.data;
                    if (result.success==false){
                      this.$message.error(result.message);
                    }
                    this.currentPage_type = 1;
                    this.addOrEditLoading_type = false;
                    this.addOrEditFormVisible_type = false;
                    this.$message.success(result.message);
                    this.getSDType();
                  })
                  .catch(result=>{
                    console.log(result);
                    this.$message.error('系统繁忙，请稍后重试!!!');
                  })
						});
					}
				});
			},
			selsChange_type: function (sels_type) {
				this.sels_type = sels_type;
			},
			//批量删除
			batchRemove_type: function () {
				var ids = this.sels_type.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
          this.$http.patch("/sdtype",ids)
            .then(result=>{
              result = result.data;
              if (result.success==false){
                this.$message.error(result.message);
              }
              this.currentPage_type = 1;
              this.getSDType();
              this.$message.success(result.message);
            })
            .catch(result=>{
              console.log(result);
              this.$message.error('系统繁忙，请稍后重试!!!');
            })
				}).catch(() => {

				});
			},
      //展示页数改变监听器、
      handleSizeChange_type(size){
        this.pageSize_type = size;
        this.getSDType();
      },
      //双击行事件监听
      dblclick_type(row, column, event){
        this.addOrEditForm_detail.types_id = row.id;
        this.getSDDetail();
        this.drawer_sdDetail = true;
      },
      //=========================================================================================

      //=========================================================================================
      //数据字典明细，方法

      //获取对应数据字典明细数据
      getSDDetail(){
        //设置分页参数，json格式
        let para = {
          currentPage: this.currentPage_detail,
          pageSize: this.pageSize_detail,
          keyword: this.filters_detail.keyword,
          type_id: this.addOrEditForm_detail.types_id
        };
        this.listLoading_detail = true;

        //发送异步请求从后端拿数据
        this.$http.post("/sddetail",para)
            .then(result=>{
              //从回调参数中获取data数据
              var data = result.data;
              this.sdDetails = data.rows;
              this.total_detail = data.total;
              this.listLoading_detail = false;
            })
            .catch(result=>{
              console.log(result);
              this.$message.error('系统繁忙，请稍后重试!!!');
            })
      },
      //选择框改变时
      selsChange_detail: function (sels_detail) {
        this.sels_detail = sels_detail;
      },
      //当前页发生改变时s
      handleCurrentChange_detail(val) {
        this.currentPage_detail = val;
        this.getSDDetail();
      },
      //显示编辑界面
      handleEdit_detail: function (index, row) {
        this.myTitle="编辑";
        this.addOrEditFormVisible_detail = true;
        //克隆  用于回显
        this.addOrEditForm_detail = Object.assign({}, row);
      },
      //显示新增界面
      handleAdd_detail: function () {
        this.myTitle="新增";
        this.addOrEditFormVisible_detail = true;
        //重置表单
        this.addOrEditForm_detail = {
          id: null,
          name: "",
          types_id:this.addOrEditForm_detail.types_id
        };
      },
      //新增或修改确认提交
      addOrEditSubmit_detail: function () {
        this.$refs.addOrEditForm_detail.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              //开启确认按钮忙等
              this.addOrEditLoading_detail = true;
              let para = Object.assign({}, this.addOrEditForm_detail);

              this.$http.put("/sddetail",para)
                  .then(result=>{
                    result = result.data;
                    if (result.success==false){
                      this.$message.error(result.message);
                    }
                    this.currentPage_detail = 1;
                    this.addOrEditLoading_detail = false;
                    this.addOrEditFormVisible_detail = false;
                    this.$message.success(result.message);
                    this.getSDDetail();
                  })
                  .catch(result=>{
                    console.log(result);
                    this.$message.error('系统繁忙，请稍后重试!!!');
                  })
            });
          }
        });
      },
      //高级查询后将当前页跳转至第一页
      highQuery_detail(){
        this.currentPage_detail = 1;
        this.getSDDetail();
      },
      //展示页数改变监听器、
      handleSizeChange_detail(size){
        this.pageSize_detail = size;
        this.getSDDetail();
      },
      //删除
      handleDel_detail: function (index, row) {
        //是否删除提示框
        this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => {   //确定
          this.listLoading_detail = true;
          //NProgress.start();
          this.$http.delete("/sddetail/"+row.id)
              .then(result=>{
                result = result.data;
                if (result.success==false){
                  this.$message.error(result.message);
                }
                //删除成功后跳转至第一页
                this.currentPage_detail = 1;
                this.getSDDetail();
                this.listLoading_detail = false;
                this.$message.success(result.message);
              })
              .catch(result=>{
                console.log(result);
                this.$message.error('系统繁忙，请稍后重试!!!');
              })
        }).catch(() => {  //取消

        });
      },
      //批量删除
      batchRemove_detail: function () {
        var ids = this.sels_detail.map(item => item.id);
        this.$confirm('确认删除选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.$http.patch("/sddetail",ids)
              .then(result=>{
                result = result.data;
                if (result.success==false){
                  this.$message.error(result.message);
                }
                this.currentPage_detail = 1;
                this.getSDDetail();
                this.$message.success(result.message);
              })
              .catch(result=>{
                console.log(result);
                this.$message.error('系统繁忙，请稍后重试!!!');
              })
        }).catch(() => {

        });
      }
		},
		mounted() {
		  //加载数据字典类型
			this.getSDType();
		}
	}

</script>

<style scoped>

</style>